package com.demo.server.controller;

import com.demo.server.service.ICachePassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
//缓存穿透实战controller
@RestController
public class CachePassController {
    private static final Logger log= LoggerFactory.getLogger(CachePassController.class);
    private static final String prefix="cache/pass";
    /**
     * 定义缓存穿透处理服务类
     */
    @Resource
    private ICachePassService ICachePassService;
    /**
     * 获取热销商品信息
     * @param itemCode
     * @return
     */
    @RequestMapping(value = prefix+"/item/info",method = RequestMethod.GET)
public Map<String,Object> getItem(@RequestParam String itemCode){
    //定义接口返回的格式，主要包括code、msg和data
        Map<String,Object> resMap=new HashMap<>();
        resMap.put("code",0);
        resMap.put("msg","成功");
        try {
            //调用缓存穿透处理服务类得到返回结果，并将其添加进结果Map中
            resMap.put("data", ICachePassService.getItemInfo(itemCode));
        }catch (Exception e){
            resMap.put("code",-1);
            resMap.put("msg","失败"+e.getMessage());
        }
        return resMap;
    }
}